import React, { useState } from 'react';
import {
  DesktopOutlined,
  FileOutlined,
  PieChartOutlined,
  TeamOutlined,
  UserOutlined,
} from '@ant-design/icons';
import type { MenuProps } from 'antd';
import { Breadcrumb, Layout, Menu, theme } from 'antd';
import {menuObj} from '../../util/type'
import { NavLink, Outlet } from 'react-router-dom';
const { Header, Content, Footer, Sider } = Layout;

type MenuItem = Required<MenuProps>['items'][number];
function getIcon(icon:string){
  switch(icon){
    case "UserOutlined":
      return <UserOutlined/>
    case "TeamOutlined":
      return <TeamOutlined />
    case "PieChartOutlined":
    return <PieChartOutlined/>
    case "FileOutlined":
    return <FileOutlined/>
  }
}
function getItem(
  path:string,
  label: React.ReactNode,
  key: React.Key,
  icon?: React.ReactNode,
  children?: MenuItem[],
): MenuItem {
  return {
    path,
    key,
    icon,
    children,
    label:<NavLink to={path}>{label}</NavLink>
  } as MenuItem;
}


type Props = {}

const Index = (props: Props) => {
  const [collapsed, setCollapsed] = useState(false);
  const {
    token: { colorBgContainer, borderRadiusLG },
  } = theme.useToken();
  const renderRoute=(arr:menuObj[])=>{
    var newArr:MenuItem[]=[]
    arr.forEach((item)=>{
      newArr.push(getItem(item.path,item.title,Math.random(),getIcon(item.icon),item.children&&renderRoute(item.children as menuObj[])))
    })
    return newArr
  }
  return (
    <Layout style={{ minHeight: '100vh' }}>
      <Sider collapsible collapsed={collapsed} onCollapse={(value) => setCollapsed(value)}>
        <div className="demo-logo-vertical" />
        <Menu theme="dark" defaultSelectedKeys={['1']} mode="inline" items={renderRoute(JSON.parse(localStorage.getItem('routes')as string))} />
      </Sider>
      <Layout>
        <Header style={{ padding: 0, background: colorBgContainer }} />
        <Content style={{ margin: '0 16px' }}>
          <Breadcrumb style={{ margin: '16px 0' }}>
            <Breadcrumb.Item>User</Breadcrumb.Item>
            <Breadcrumb.Item>Bill</Breadcrumb.Item>
          </Breadcrumb>
          <div
            style={{
              padding: 24,
              minHeight: 360,
              background: colorBgContainer,
              borderRadius: borderRadiusLG,
            }}
          >
           <Outlet></Outlet>
          </div>
        </Content>
        <Footer style={{ textAlign: 'center' }}>
          Ant Design ©{new Date().getFullYear()} Created by Ant UED
        </Footer>
      </Layout>
    </Layout>
  )
}

export default Index